<!--
 * @Description: 模版管理
 * @Author: 杨帆
-->
<template>
  <x-page>
    <!-- 表格过滤 -->
    <x-filter @search="handleSearch" @reset="resetSearch">
      <x-filter-item :label="$t('resourceAdmin.template.templateCode')">
        <el-input
          v-model="searchParams.title"
          :placeholder="$t('resourceAdmin.template.titlePlaceholder')"
          clearable
        ></el-input>
      </x-filter-item>
      <x-filter-item :label="$t('resourceAdmin.template.templateName')">
        <el-input
          v-model="searchParams.module"
          :placeholder="$t('resourceAdmin.template.modulePlaceholder')"
          clearable
        ></el-input>
      </x-filter-item>
    </x-filter>
    <!--数据面板-->
    <x-data>
      <!--数据面板按钮插糟-->
      <template #tool>
        <el-row type="flex" class="tools" justify="space-between">
          <el-col :span="20">
            <el-button
              v-hasPermi="['upms:template:add']"
              size="small"
              type="primary"
              :icon="buttonIcons('add')"
              @click="add"
            >
              {{ $t('common.add') }}
            </el-button>
          </el-col>
          <el-col :span="4" class="tools-config">
            <x-column-config :columns="columns" @updateColumns="updateColumns" @on-refresh="loadData" />
          </el-col>
        </el-row>
      </template>
      <!-- 数据表格 -->
      <x-table
        ref="tableCom"
        :pagination="pagination"
        :data-source="tableData"
        :loading="loading"
        :row-key="'id'"
        @on-change="handleTableChange"
      >
        <el-table-column :label="$t('common.serialNumber')" :width="55" fixed type="index" />
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.prop"
          :fixed="item.fixed"
          :show-overflow-tooltip="true"
          :width="item.width"
          :min-width="item.minWidth || '-'"
          :label="item.label"
          :sortable="'custom'"
        >
          <template #default="scope">
            <el-switch
              v-if="item.prop === 'state'"
              :value="scope.row[item.prop]"
              active-color="#4474F6"
              inactive-color="#A5A9B2"
              @change="handleChangeState(scope.row[item.prop], scope.row.id)"
              :disabled="!checkBntPermi('upms:template:state')"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <!-- <x-state-cell
              v-if="item.prop === 'state'"
              :stateClass="scope.row[item.prop] === 0 ? 'gray' : scope.row[item.prop] ? 'success' : ''"
              :text="selectDictLabel(dictOptions['system.state'], scope.row[item.prop] + '')"
            /> -->
            <span v-else>{{ scope.row[item.prop] | emptyVal }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" :label="$t('common.opeartion')" :align="'center'" width="160">
          <template #default="scope">
            <more-table-actions
              :action-list="actions"
              :permis-list="permissionsActions"
              :row="scope.row"
              @onActionClick="handleActions"
            />
          </template>
        </el-table-column>
      </x-table>
    </x-data>
    <template-dialog :dictOptions="dictOptions" ref="templateDialog" @handleSuccess="loadData"></template-dialog>
  </x-page>
</template>
<script>
  // import { QueryDictMixin, TableListMixin, PermissionsMixin } from '@/common/mixins/index.js'
  import { selectDictLabel, buttonIcons, QueryDictMixin, TableListMixin, PermissionsMixin } from '@package/utils'
  import { columns, actions } from '../config/templateConfig.js'
  import { listUrl, apiDeleteTemplate, apiPutEnableTemplate, apiPutDisableTemplate } from '../api/template.js'
  import templateDialog from '../components/templateDialog.vue'

  import { EnumApiCode } from '../../../../../../globalEnum'

  export default {
    name: 'Template',
    components: { templateDialog },
    mixins: [TableListMixin, QueryDictMixin, PermissionsMixin],
    data() {
      return {
        columns,
        listUrl,
        actions,
        searchParams: {
          templateCode: '',
          templateName: ''
        },
        statusOptions: [],
        tableColumns: [],
        tableData: [],
        pagination: {
          pageSize: 10,
          pageNum: 1
        },
        selectionData: [],
        dictOptions: {},
        dictKeys: ['system.state']
      }
    },
    created() {
      this.initDictConfig()
    },
    methods: {
      buttonIcons,
      selectDictLabel,
      handleActions({ key, data }) {
        switch (key) {
          case 'edit':
            this.$refs.templateDialog.handleOpen(data)
            break
          case 'delete':
            this.$confirm(
              this.$t('resourceAdmin.template.deletConfim'),
              this.$t('resourceAdmin.template.deletePrompt'),
              {
                confirmButtonText: this.$t('common.confirm'),
                cancelButtonText: this.$t('common.cancel'),
                type: 'warning'
              }
            )
              .then((confirm) => {
                this.handleDeleteTemplate(data.id)
              })
              .catch((cancel) => {})
            break
          default:
            break
        }
      },
      add() {
        this.$refs.templateDialog.handleOpen()
      },
      // 删除
      handleDeleteTemplate(id) {
        apiDeleteTemplate(id).then((code, msg) => {
          if (code === EnumApiCode.Success) {
            this.$message.success(this.$t('system.user.successTips'))
            this.loadData()
          } else {
            this.$message.error(msg)
          }
        })
      },
      // 状态切换
      async handleChangeState(state, ids) {
        const result = Array.isArray(ids) ? ids : [ids]
        try {
          const { code, msg } =
            `${state}` === '0' ? await apiPutEnableTemplate(result) : await apiPutDisableTemplate(result)
          if (code === EnumApiCode.Success) {
            this.$message.success(this.$t('system.user.successTips'))
          } else {
            this.$message.error(msg)
          }
          this.loadData()
        } finally {
          this.loadData()
        }
      }
    }
  }
</script>
